package xxy.com.customview.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import xxy.com.customview.R;

/**
 * Created by Administrator on 2017/12/12.
 */

public class MyBehavior extends CoordinatorLayout.Behavior {

    private int childHeight;
    private final int SCROOL_VALUE = 50;   //滑动阈值
    private final int animationDuration = 500;
    private boolean isAnimating = false;
    private boolean isHide = false;

    /**
     * 实现NestedScrollingParent的coordinatorlayout类会把从实现了NestedScrollingchild的类中传递过来的触摸事件传递给实现behavior的类（这个类不实现NestedScrollingchild）
     * @param context
     * @param attrs
     */
    public MyBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 这里的onStartNestedScroll是coordinatorlayout中的方法
     * @param coordinatorLayout
     * @param child
     * @param directTargetChild
     * @param target
     * @param axes
     * @param type
     * @return
     */
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        if (target.getId() == R.id.main6_view_two) {   //判断传过来的触摸事件是从哪个view传过来的
            if (childHeight == 0) {
                childHeight = child.getHeight();   //子孩子也就是本类的高度是个定值，这里是700
                Log.e("MyBehavior", "childHeight:\t" + childHeight);
            }
            return true;   //返回true代表处理
        } else return false;
    }

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        if (isAnimating) {   //如果在动画当中就返回，不做任何处理
            return;
        }
        Log.e("dy", "dy:\t" + dy);
        if (dy < - SCROOL_VALUE  && isHide) {   //向下滑动的距离小于设置的阈值，并且是影藏的，则显示view
            show(child, target);
        } else if (dy > SCROOL_VALUE  && !isHide) {
            hide(child, target);
        }
    }

    private void hide(final View child, final View target) {
        isHide = true;
        ValueAnimator valueAnimator = new ValueAnimator();   //这里用值动画
        valueAnimator.setIntValues(0, childHeight);   //给定两个值，让值动画在这两个值之间做动画
        valueAnimator.setDuration(animationDuration);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {   //值动画一般都要设置监听，作用是值动画在0到childheight两个值的变化时，根据每一个值的变化做出自己的逻辑
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.e("bottom", "child.getBottom():\t" + child.getBottom() + "\nchild.gettop():\t" + child.getTop());
                if (child.getBottom() > 0) {   //本类的底部大于0，也就是在屏幕中显示出来了
                    int animatedValue = (int)animation.getAnimatedValue();   //得到当前值动画变化的值是多少
                    Log.e("MyBehavior", "animatedValueHide:\t" + animatedValue);
                    isAnimating = animatedValue != childHeight;   //为true表示还在动画中
                    //重新并且永久的设置view的布局参数，点击事件在view改变后也是起作用的
                    child.layout(child.getLeft(), - animatedValue, child.getRight(), - animatedValue + childHeight);
                    target.layout(target.getLeft(), - animatedValue + childHeight, target.getRight(), target.getBottom());
                }
            }
        });
        valueAnimator.start();
    }

    private void show(final View child, final View target) {
        isHide = false;
        ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setIntValues(0, childHeight);
        valueAnimator.setDuration(animationDuration);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.e("bottom", "child.getBottom():\t" + child.getBottom() + "\nchild.gettop():\t" + child.getTop());
                if (child.getBottom() < childHeight) {
                    int animatedValue = (int)animation.getAnimatedValue();
                    Log.e("MyBehavior", "animatedValueShow:\t" + animatedValue);
                    isAnimating = animatedValue != childHeight;
                    child.layout(child.getLeft(), animatedValue - childHeight, child.getRight(), animatedValue);
                    target.layout(target.getLeft(), animatedValue, target.getRight(), target.getBottom());
                }
            }
        });
        valueAnimator.start();
    }
}
